home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Games / Pentominoes 2.0 / Shell ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-15  |  8.7 KB  |  353 lines  |  [TEXT/MMCC]

  1. #include "main.h"
  2. #include "program globals.h"
  3. #include "key layer.h"
  4. #if USE_DRAG
  5. #include "drag layer.h"
  6. #endif
  7. #include "integrity.h"
  8. #include "menus.h"
  9. #include "prefs.h"
  10. #include "environment.h"
  11. #include "error.h"
  12. #if USE_PRINTING
  13. #include "printing layer.h"
  14. #endif
  15. #include "graphics dispatch.h"
  16. #if USE_SOUNDS
  17. #include "sound layer.h"
  18. #endif
  19. #if USE_MUSIC
  20. #include "music layer.h"
  21. #endif
  22. #if USE_SPEECH
  23. #include "speech layer.h"
  24. #endif
  25. #include "window layer.h"
  26. #include "program init.h"
  27. #if USE_MERCUTIO
  28. #include "Mercutio API.h"
  29. #endif
  30.  
  31. void main(void)
  32. {
  33.     Boolean            programIntegrityVerified;
  34.     Boolean            programIntegritySet;
  35.     ErrorTypes        resultCode;
  36.     
  37.     /* standard program initialization stuff */
  38.     MaxApplZone();    
  39.     InitGraf(&qd.thePort);
  40.     InitFonts();
  41.     FlushEvents(everyEvent, 0);
  42.     InitWindows();
  43.     InitMenus();
  44.     TEInit();
  45.     InitDialogs(0L);
  46.     InitCursor();
  47.     GetDateTime((unsigned long*)&qd.randSeed);
  48.     
  49.     /* do integrity check before anything else; see integrity.c for details */
  50.     programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
  51.     
  52.     if ((resultCode=InitTheEnvironment())!=allsWell)
  53.         HandleError(resultCode, TRUE, TRUE);
  54.     
  55.     if (!programIntegrityVerified)    /* integrity check failed */
  56.         HandleError(kProgramIntegrityNotVerified, TRUE, FALSE);
  57.     
  58.     if (programIntegritySet)    /* integrity check freshly installed */
  59.         HandleError(kProgramIntegritySet, FALSE, TRUE);
  60.     
  61. #if USE_DRAG
  62.     InitTheDragManager();
  63. #endif
  64. #if USE_SOUNDS
  65.     InitTheSounds();
  66. #endif
  67. #if USE_MUSIC
  68.     InitTheMusic();
  69. #endif
  70. #if USE_SPEECH
  71.     InitTheSpeech();
  72. #endif
  73.  
  74.     PrefsError(PreferencesInit());    /* get prefs (create if necessary) */
  75.     
  76.     if (!InitTheMenus())        /* get menus from .rsrc and draw menu bar */
  77.         HandleError(kProgramIntegrityNotVerified, TRUE, FALSE);
  78.         
  79. #if USE_PRINTING
  80.     InitThePrinting();
  81. #endif
  82.     
  83.     InitTheProgram();
  84.     
  85.     EventLoop();                    /* where it all happens (see below) */
  86.     
  87.     ShutDownEnvironment(TRUE);        /* where it all ends (see below) */
  88.     
  89.     ExitToShell();
  90. }
  91.  
  92. void EventLoop(void)
  93. {
  94.     while (!gDone)    /* gDone set by choosing "Quit" menu item or by "quit" apple event */
  95.         HandleSingleEvent(TRUE);
  96. }
  97.  
  98. Boolean HandleSingleEvent(Boolean allowContextSwitching)
  99. {
  100.     EventRecord        theEvent;
  101.     WindowRef        front;
  102.     
  103.     if (!gCustomCursor)
  104.         SetCursor(&qd.arrow);
  105.     HiliteMenu(0);            /* normalize menubar */
  106.     
  107.     gFrontWindowIndex=-1;
  108.     front=FrontWindow();
  109.     if (front!=0L)    /* if there's a front window, see if it's one of ours */
  110.     {
  111.         SetPort(front);
  112.         gFrontWindowIndex=GetWindowIndex(front);
  113.     }
  114.         
  115.     /* get an event from the queue */
  116.     WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? gBackgroundWaitTime : gForegroundWaitTime, 0L);
  117.     SaveEventModifiers(&theEvent);
  118.     
  119.     DispatchEvents(theEvent, allowContextSwitching);    /* handle the event we just got */
  120.     
  121.     return (theEvent.what!=nullEvent);
  122. }
  123.  
  124. void DispatchEvents(EventRecord theEvent, Boolean allowContextSwitching)
  125. {
  126.     Point            thisPoint;
  127.     WindowRef        theWindow;
  128.     unsigned char    theChar;
  129.     long            dummy;
  130.     
  131.     switch (theEvent.what)
  132.     {
  133.         case nullEvent:    /* ain't nuthin' happenin' */
  134. #if USE_SOUNDS
  135.             if ((gSoundAvailable) && (gSoundIsFinishedPlaying))
  136.                 CloseTheSoundChannel();
  137. #endif
  138.             if (gKludgeIter<3)
  139.             {
  140.                 gKludgeIter++;
  141.             }
  142.             else
  143.             {
  144.                 if (gNeedToOpenWindow)
  145.                 {
  146.                     OpenTheIndWindow(kMainWindow, kAlwaysOpenNew);
  147.                     gNeedToOpenWindow=FALSE;
  148.                 }
  149.             }
  150.             
  151.             thisPoint=theEvent.where;
  152.             GlobalToLocal(&thisPoint);
  153.             theWindow=FrontWindow();
  154.             if (WindowIsFloat(theWindow))
  155.             {
  156.                 if (IdleWindowDispatch(theWindow, thisPoint)==kPassThrough)
  157.                 {
  158.                     if ((theWindow=GetFrontDocumentWindow())!=0L)
  159.                     {
  160.                         SetPort(theWindow);
  161.                         thisPoint=theEvent.where;
  162.                         GlobalToLocal(&thisPoint);
  163.                         IdleWindowDispatch(theWindow, thisPoint);
  164.                     }
  165.                 }
  166.             }
  167.             else
  168.             {
  169.                 IdleWindowDispatch(theWindow, thisPoint);
  170.             }
  171.             break;
  172.         case mouseDown:    /* mouse button pressed */
  173.             HandleMouseDown(theEvent, allowContextSwitching);    /* see below for mousedown handling */
  174.             break;
  175.         case mouseUp:    /* mouse button released */
  176.             MouseUpDispatch(GetFrontDocumentWindow());
  177.             break;
  178.         case keyDown:    /* key pressed */
  179.         case autoKey:    /* key help down */
  180.             theChar=(char)(theEvent.message & charCodeMask);
  181.             if (theEvent.modifiers & cmdKey)
  182.             {
  183.                 if ((theChar=='∑') || (theChar=='„'))
  184.                 {    /* hack to map cmd-(option-w) to cmd-w + option key */
  185.                     theEvent.modifiers|=optionKey;
  186.                     SaveEventModifiers(&theEvent);
  187.                     theChar='w';
  188.                 }
  189.                 AdjustMenus();
  190. #if USE_MERCUTIO
  191.                 dummy=PowerMenuKey(theEvent.message, theEvent.modifiers, gBuildMenu);
  192.                 if (dummy==0L)
  193. #endif
  194.                     dummy=MenuKey(theChar);
  195.                 HandleMenu(dummy);
  196.             }
  197.             else
  198.             {
  199.                 theWindow=FrontWindow();
  200.                 if (WindowIsFloat(theWindow))
  201.                 {
  202.                     if (KeyDownDispatch(theWindow, theChar)==kPassThrough)
  203.                     {
  204.                         if ((theWindow=GetFrontDocumentWindow())!=0L)
  205.                         {
  206.                             KeyDownDispatch(theWindow, theChar);
  207.                         }
  208.                     }
  209.                 }
  210.                 else
  211.                 {
  212.                     KeyDownDispatch(theWindow, theChar);
  213.                 }
  214.             }
  215. #if USE_DRAG
  216.             ResetHiliteRgn(theWindow);
  217. #endif
  218.             break;
  219.         case diskEvt:    /* disk insert */
  220.             if (HiWord(theEvent.message)!=noErr)    /* bad disk inserted */
  221.             {
  222.                 DILoad();    /* load disk initialization package */
  223.                 SetPt(&thisPoint, 120, 120);
  224.                 DIBadMount(thisPoint, theEvent.message);    /* give format? dialog */
  225.                 DIUnload();    /* unload 'cuz we certainly don't need it */
  226.             }
  227.             break;
  228.         case updateEvt:    /* window update */
  229.             theWindow=(WindowRef)theEvent.message;    /* which window? */
  230.             BeginUpdate(theWindow);        /* means: "OK, we're dealing with this now" */
  231.             UpdateTheWindow(theWindow);
  232.             EndUpdate(theWindow);        /* means: "OK, we're done updating now" */
  233.             break;
  234.         case activateEvt:    /* window activate or deactivate */
  235.             theWindow=(WindowRef)theEvent.message;
  236.             if ((theEvent.modifiers & activeFlag)!=0)
  237.                 ActivateWindowDispatch(theWindow);
  238.             else
  239.                 DeactivateWindowDispatch(theWindow);
  240.             break;
  241.         case osEvt:            /* suspend or resume program execution (switch in/out) */
  242.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  243.             {
  244.                 /* keep track of whether we're in the background or foreground */
  245.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  246.                 
  247.                 if (gIsInBackground)
  248.                 {
  249.                     SuspendFloatingWindows();
  250. #if USE_MUSIC
  251.                     CloseTheMusicChannel();
  252. #endif
  253.                 }
  254.                 else
  255.                 {
  256.                     ResumeFloatingWindows();
  257. #if USE_MUSIC
  258.                     if (gMusicToggle)
  259.                         StartTheMusic();
  260. #endif
  261.                 }
  262.                 
  263.                 /* if we just came into the foreground and we have a pending error,
  264.                    now's the time to display it */
  265.                 if ((!gIsInBackground) && (gPendingErrorRec.resultCode!=allsWell))
  266.                 {
  267.                     if (gHasNotificationManager)
  268.                         NMRemove(&gPendingErrorRec.notificationRec);        /* remove notification request */
  269.                     HandleError(gPendingErrorRec.resultCode, gPendingErrorRec.isFatal,
  270.                         gPendingErrorRec.isSmall);    /* display alert, see error.c */
  271.                     gPendingErrorRec.resultCode=allsWell;        /* ...now it is */
  272.                 }
  273.             }
  274.             break;
  275.         case kHighLevelEvent:    /* apple event */
  276.             AEProcessAppleEvent(&theEvent);        /* see apple events.c */
  277.             break;
  278.     }
  279. }
  280.  
  281. void HandleMouseDown(EventRecord theEvent, Boolean allowContextSwitching)
  282. {
  283.     WindowRef        theWindow;
  284.     short            windowCode;
  285.     Point            theLocalPoint;
  286.     
  287.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  288.     
  289.     switch (windowCode)
  290.     {
  291.         case inMenuBar:        /* in menu bar; let system take over */
  292.             AdjustMenus();
  293.             HandleMenu(MenuSelect(theEvent.where));
  294.             break;
  295.         case inContent:        /* in window content */
  296. #if USE_DRAG
  297.             if (!DragInWindow(theWindow, &theEvent))
  298.             {
  299. #endif
  300.                 if (!MySelectWindow(theWindow))        /* didn't need to change front windows */
  301.                 {
  302.                     theLocalPoint=theEvent.where;
  303.                     SetPort(theWindow);
  304.                     GlobalToLocal(&theLocalPoint);
  305.                     MouseDownDispatch(theWindow, theLocalPoint);
  306.                 }
  307. #if USE_DRAG
  308.             }
  309.             ResetHiliteRgn(theWindow);
  310. #endif
  311.             break;
  312.         case inSysWindow:    /* in system window (desk accessory) */
  313.             if (allowContextSwitching)
  314.                 SystemClick(&theEvent, theWindow);    /* let the system deal with it */
  315.             break;
  316.         case inDrag:        /* in drag _region_, that is */
  317.             MyDragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
  318.             break;
  319.         case inGoAway:        /* close box */
  320.             /* the accepted way to track a close box attempt */
  321.             if (TrackGoAway(theWindow, theEvent.where))
  322.                 DoTheCloseThing((WindowPeek)theWindow);        /* see menus.c */
  323.             break;
  324.         case inGrow:        /* grow box */
  325.             MyGrowWindow(theWindow, theEvent.where);
  326.             break;
  327.         case inZoomIn:        /* zoom box */
  328.         case inZoomOut:
  329.             /* the accepted way to track a zoom attempt */
  330.             if (TrackBox(theWindow, theEvent.where, windowCode))
  331.             {
  332.                 MyZoomWindow(theWindow, windowCode);
  333.             }
  334.             break;
  335.     }
  336. }
  337.  
  338. void ShutDownEnvironment(Boolean fullShutdown)
  339. {
  340.     SaveThePrefs();
  341.     if (fullShutdown)
  342.     {
  343.         ShutDownTheProgram();        /* program-specific cleanup */
  344.         ShutDownTheMenus();            /* release menu resources */
  345.     }
  346. #if USE_SOUNDS
  347.     CloseTheSoundChannel();
  348. #endif
  349. #if USE_MUSIC
  350.     CloseTheMusicChannel();
  351. #endif
  352. }
  353.